<template>
  <div id="user-manager-page">
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item>
        <a-input v-model:value="searchParams.userAccount" placeholder="账号" allowClear> </a-input>
      </a-form-item>
      <a-form-item>
        <a-input v-model:value="searchParams.userName" placeholder="用户昵称" allowClear> </a-input>
      </a-form-item>
      <a-form-item>
        <a-input v-model:value="searchParams.userRole" placeholder="用户权限" allowClear> </a-input>
      </a-form-item>
      <a-form-item>
        <a-input v-model:value="searchParams.userProfile" placeholder="用户简介" allowClear>
        </a-input>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit"> 搜索 </a-button>
        <a-button type="primary" danger @click="doClear()" style="margin-left: 16px">
          清空
        </a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <a-table
      :columns="columns"
      :data-source="dataSource"
      :pagination="pagination"
      @change="handleTableChange"
    >
      <template #headerCell="{ column }">
        <template v-if="column.key === 'name'">
          <span> Name </span>
        </template>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAccount'">
          {{ record?.userAccount }}
        </template>
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :width="120" :src="record?.userAvatar" />
        </template>
        <template v-if="column.dataIndex === 'userProfile'">
          {{ record?.userProfile }}
        </template>
        <template v-if="column.dataIndex === 'userRole'">
          <a-tag :color="record?.userRole === 'admin' ? 'blue' : 'green'">
            {{ record?.userRole }}
          </a-tag>
        </template>
        <template v-if="column.key === 'action'">
          <div class="button_box">
            <a-space>
              <a-button type="primary" shape="circle" @click="doEdit(record.id)">
                <template #icon>
                  <EditFilled />
                </template>
              </a-button>
              <a-button type="primary" danger shape="circle" @click="doDelete(record.id)">
                <template #icon>
                  <DeleteFilled />
                </template>
              </a-button>
            </a-space>
          </div>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref, reactive, computed } from 'vue'
import { listUserByPageUsingPost } from '@/request/api/userController'
import { message } from 'ant-design-vue'
import { EditFilled, DeleteFilled } from '@ant-design/icons-vue'
const searchParams = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
  userName: '',
  userAccount: '',
  userProfile: '',
  userRole: '',
})

let total = ref(0)
let dataSource = ref<API.UserVO[]>([])

const pagination = computed(() => ({
  total: Number(total.value),
  current: searchParams.pageNum,
  pageSize: searchParams.pageSize,
  showSizeChanger: true,
  showTotal: (total: number) => `共${total}条`,
}))

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    align: 'center',
  },

  {
    title: '账号',
    dataIndex: 'userAccount',
    align: 'center',
  },
  {
    title: '用户昵称',
    dataIndex: 'userName',
    align: 'center',
  },

  {
    title: '用户头像',
    dataIndex: 'userAvatar',
    align: 'center',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
    align: 'center',
  },
  {
    title: '用户简介',
    dataIndex: 'userProfile',
    align: 'center',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    align: 'center',
  },
  {
    title: '操作',
    key: 'action',
  },
]
// 获取数据
const featureData = async () => {
  try {
    const { data: res } = await listUserByPageUsingPost({ ...searchParams })
    if (res?.code === 0 && res?.data) {
      total.value = res.data.total || 0
      dataSource.value = res.data.records || []
    } else {
      message.error(res?.message || '获取数据失败')
    }
  } catch (error) {
    message.error('获取数据失败')
    console.error(error)
  }
}
const handleTableChange = (pagination: any) => {
  console.log(pagination)
  // 表数据发生改变时
  searchParams.pageSize = pagination.pageSize
  searchParams.pageNum = pagination.current
  featureData()
}
const doSearch = () => {
  // 搜索
  searchParams.pageNum = 1
  featureData()
}
const doClear = () => {
  // 清空
  searchParams.userAccount = ''
  searchParams.userProfile = ''
  searchParams.userRole = ''
  searchParams.pageNum = 1
  featureData()
}
const doEdit = (id: number) => {
  // 编辑用户
  console.log('编辑用户', id)
}
const doDelete = (id: number) => {
  // 删除用户
  console.log(id)
}

// 页面加载时调用
onMounted(() => {
  featureData()
})
</script>
<style lang="less" scoped></style>
